# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=kodi
pkgver=18.7.1
_realname=Leia
pkgrel=0
_libdvdcss_pkgver="1.4.2-Leia-Beta-5"
_libdvdread_pkgver="6.0.0-Leia-Alpha-3"
_libdvdnav_pkgver="6.0.0-Leia-Alpha-3"
_crossguid_hash="8f399e8bd4"
pkgdesc="A software media player and entertainment hub for digital media"
url="https://kodi.tv"
arch="x86 x86_64 !armhf armv7 aarch64" # flatbuffers missing on armhf
license="GPL-2.0-only"
depends="python2
	hicolor-icon-theme
	xdpyinfo
	curl
	py-pillow
	py-bluez
	py-simplejson"
# Required dependencies
depends_dev="libass-dev
	libcdio-dev
	curl-dev
	ffmpeg-dev
	flatbuffers-dev
	fmt-dev
	freetype-dev
	fribidi-dev
	fstrcmp-dev
	libdvdcss-dev
	lzo-dev
	openssl-dev
	pcre-dev
	rapidjson-dev
	sqlite-dev
	taglib-dev
	tinyxml-dev
	zlib-dev
	giflib-dev
	mesa-dev
	glu-dev
	libjpeg-turbo-dev
"

# Required for Wayland
depends_dev="$depends_dev
	waylandpp-dev
	libxkbcommon-dev
"

# Required for gbm
depends_dev="$depends_dev
	libinput-dev
"

# Optional for extra features
depends_dev="$depends_dev
	alsa-lib-dev
	avahi-dev
	bluez-dev
	libbluray-dev
	libcap-dev
	libcec-dev
	dbus-dev
	libmicrohttpd-dev
	libnfs-dev
	libplist-dev
	pulseaudio-dev
	python2-dev
	samba-dev
	eudev-dev
	libxslt-dev
	mariadb-connector-c-dev
	libshairport-dev
	libva-glx-dev"
makedepends="$depends_dev
	cmake
	swig
	autoconf
	automake
	libtool
	doxygen
	graphviz
	xz
	tar
	openjdk8-jre-base
	wayland-protocols"
subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc $pkgname-dbg $pkgname-bin $pkgname-wayland $pkgname-gbm $pkgname-polkit::noarch"
replaces="xbmc"
options="ldpath-recursive !check" # No testsuite
source="https://github.com/xbmc/xbmc/archive/$pkgver-$_realname.tar.gz
	libdvdcss-$_libdvdcss_pkgver.tar.gz::https://github.com/xbmc/libdvdcss/archive/$_libdvdcss_pkgver.tar.gz
	libdvdread-$_libdvdread_pkgver.tar.gz::https://github.com/xbmc/libdvdread/archive/$_libdvdread_pkgver.tar.gz
	libdvdnav-$_libdvdnav_pkgver.tar.gz::https://github.com/xbmc/libdvdnav/archive/$_libdvdnav_pkgver.tar.gz
	crossguid-$_crossguid_hash.tar.gz::https://mirrors.kodi.tv/build-deps/sources/crossguid-$_crossguid_hash.tar.gz
	set-default-stacksize.patch
	fix-musl-incompability.patch
	0001-Compatibility-with-libmicrohttpd-0.9.71.patch
	kodi.initd
	60-kodi.rules
	"

builddir="$srcdir/xbmc-$pkgver-$_realname"

# secfixes:
#   18.2-r0:
#     - CVE-2018-8831

prepare() {
	default_prepare

	mkdir "$builddir"/build-x11
	mkdir "$builddir"/build-wayland
	mkdir "$builddir"/build-gbm
}

build() {
	make -C tools/depends/target/crossguid PREFIX="$PWD"/usr

	# X11
	cd "$builddir"/build-x11
	cmake "$builddir" \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DENABLE_INTERNAL_CROSSGUID=ON \
		-DENABLE_INTERNAL_FFMPEG=OFF \
		-DENABLE_INTERNAL_RapidJSON=OFF \
		-DENABLE_INTERNAL_FMT=OFF \
		-DENABLE_INTERNAL_FSTRCMP=OFF \
		-DENABLE_INTERNAL_FLATBUFFERS=OFF \
		-Dlibdvdcss_URL="$srcdir"/libdvdcss-$_libdvdcss_pkgver.tar.gz \
		-Dlibdvdread_URL="$srcdir"/libdvdread-$_libdvdread_pkgver.tar.gz \
		-Dlibdvdnav_URL="$srcdir"/libdvdnav-$_libdvdnav_pkgver.tar.gz \
		-DCROSSGUID_URL="$srcdir"/crossguid-$_crossguid_hash.tar.gz
	make
	make preinstall

	# Wayland
	cd "$builddir"/build-wayland
	cmake "$builddir" \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DENABLE_INTERNAL_CROSSGUID=ON \
		-DENABLE_INTERNAL_FFMPEG=OFF \
		-DENABLE_INTERNAL_RapidJSON=OFF \
		-DENABLE_INTERNAL_FMT=OFF \
		-DENABLE_INTERNAL_FSTRCMP=OFF \
		-DENABLE_INTERNAL_FLATBUFFERS=OFF \
		-Dlibdvdcss_URL="$srcdir"/libdvdcss-$_libdvdcss_pkgver.tar.gz \
		-Dlibdvdread_URL="$srcdir"/libdvdread-$_libdvdread_pkgver.tar.gz \
		-Dlibdvdnav_URL="$srcdir"/libdvdnav-$_libdvdnav_pkgver.tar.gz \
		-DCROSSGUID_URL="$srcdir"/crossguid-$_crossguid_hash.tar.gz \
		-DCORE_PLATFORM_NAME=wayland \
		-DWAYLAND_RENDER_SYSTEM=gl
	make
	make preinstall

	# GBM
	cd "$builddir"/build-gbm
	cmake "$builddir" \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DENABLE_INTERNAL_CROSSGUID=ON \
		-DENABLE_INTERNAL_FFMPEG=OFF \
		-DENABLE_INTERNAL_RapidJSON=OFF \
		-DENABLE_INTERNAL_FMT=OFF \
		-DENABLE_INTERNAL_FSTRCMP=OFF \
		-DENABLE_INTERNAL_FLATBUFFERS=OFF \
		-Dlibdvdcss_URL="$srcdir"/libdvdcss-$_libdvdcss_pkgver.tar.gz \
		-Dlibdvdread_URL="$srcdir"/libdvdread-$_libdvdread_pkgver.tar.gz \
		-Dlibdvdnav_URL="$srcdir"/libdvdnav-$_libdvdnav_pkgver.tar.gz \
		-DCROSSGUID_URL="$srcdir"/crossguid-$_crossguid_hash.tar.gz \
		-DCORE_PLATFORM_NAME=gbm \
		-DGBM_RENDER_SYSTEM=gles
	make
	make preinstall
}

package() {
	depends="$pkgname-bin"

	cd "$builddir"/build-x11
	make DESTDIR="$pkgdir" install

	install -Dm755 "$srcdir"/kodi.initd "$pkgdir"/etc/init.d/kodi

	rm \
		"$pkgdir"/usr/lib/kodi/kodi-x11 \
		"$pkgdir"/usr/lib/kodi/kodi-xrandr
}

bin() {
	pkgdesc="X11 Kodi binary"

	cd "$builddir"/build-x11
	install -Dm755 kodi-x11 "$subpkgdir"/usr/lib/kodi/kodi-x11
	install -Dm755 kodi-xrandr "$subpkgdir"/usr/lib/kodi/kodi-xrandr
}

wayland() {
	pkgdesc="Wayland Kodi binary"
	provides="$pkgname-bin"

	cd "$builddir"/build-wayland
	install -Dm755 kodi-wayland "$subpkgdir"/usr/lib/kodi/kodi-wayland
}

gbm() {
	pkgdesc="GBM Kodi binary"
	provides="$pkgname-bin"
	depends="$pkgname-openrc"
	install="$subpkgname.pre-install"

	cd "$builddir"/build-gbm
	install -Dm755 kodi-gbm "$subpkgdir"/usr/lib/kodi/kodi-gbm
}

polkit() {
	pkgdesc="Polkit rules for Kodi in combination with GBM"
	install_if="$pkgname-gbm polkit"

	install -Dm644 "$srcdir"/60-kodi.rules "$subpkgdir"/etc/polkit-1/rules.d/60-kodi.rules
}

sha512sums="db44c2d3c998af37948ccd042932545c9d781b622dba85ec11b7dd97ec0be80f03d2687249a9483d1b2a7178179bf5a003f8d0f4c0e55c9f2a41f67a27a19e0f  18.7.1-Leia.tar.gz
5185dbdbeb1bd13ea9d8723f1f4ab599d6f3102f5ba1096cd085aa1cda252c045f327c719227bba8e1b742352ade5e335106c8d0c1637a5a6b93ce661620dd7e  libdvdcss-1.4.2-Leia-Beta-5.tar.gz
b3419ba0a1a2dd70f1bb6236afdfe1c6e88c9ad4264198b289e3bba9375e077cecf7f89848c7b09debaa445327f3507101f3d157e692f7a7163b2bb52643e1e7  libdvdread-6.0.0-Leia-Alpha-3.tar.gz
11c93eaacd156f8fd7dec7c43d366438b201f31ad55b2870463a9e286912b6ada08882319a021fb7992190f87b909a49f2b83e0321cc17aedc29f7fe5898fa72  libdvdnav-6.0.0-Leia-Alpha-3.tar.gz
2682d63609d3dcdfcd8136be632e45df26ad88ce93b9c49745cf728bbd2e6254a7b05c8b059ab581d532372e504206a525a52564b64d076dfdae9c965a09fd16  crossguid-8f399e8bd4.tar.gz
3fd97e9fa22e59b3cd30fdad19474e177a9d062598765fa8ba4955f1da4e10581ff1463dd0794d3ee3cefebf753007a4d445b2bc47a257574d3927c1408b8876  set-default-stacksize.patch
65b78a96bef33cec975b371f1a930d551d7b224955d6075b7edc7e51ec30fec9c77d34f622abbaf827c60df6029206b72c02ee2d2f138d276c1b843336fd14ab  fix-musl-incompability.patch
eb54ade14460c7b19d56d61abb79963e26131135f36492a736ec1c9ec0f34f373dbcadbd0e687e6e872c8bfc34b070dc8d7f1be571fc91767459b817df7ef43b  0001-Compatibility-with-libmicrohttpd-0.9.71.patch
7e2a2289da3098e0007e461ad538dcd6435e908283caf1719788248fe736615890175e11392ca0cd68002872217bbf7b70cf909f228307828aa82467d5502ed4  kodi.initd
465a2079d598bf909a458d8e9d0ddd2b2a41dff83ba736542cffec7811c044295d5af092928f20aaa6cd3bc8b13bb03ede5c7ded1783680f890464e103392787  60-kodi.rules"
